import 'package:flutter/material.dart';

class DropDownTextField extends StatefulWidget {
  final double? height;
  final double? width;
  final Color? backgroundColor;
  final Color? boardColor;
  final EdgeInsets? margin;
  final EdgeInsets? padding;
  final TextEditingController? controller;
  final TextStyle? hintStyle;
  final TextStyle? textStyle;
  final EdgeInsets? contentPadding;
  final Widget? icon;
  final Function? onPressed;
  final List<dynamic>? list;
  final TextInputType? textInputType;
  final Color? dialogColor;
  final TextStyle? dialogTextStyle;
  final double? dialogItemHeight;

  DropDownTextField(
      {this.height,
      this.width,
      this.backgroundColor,
      this.boardColor,
      this.margin,
      this.padding,
      this.controller,
      this.hintStyle,
      this.textStyle,
      this.contentPadding,
      this.icon,
      this.onPressed,
      this.list,
      this.textInputType,
      this.dialogColor,
      this.dialogTextStyle,
      this.dialogItemHeight});

  @override
  State<StatefulWidget> createState() => _DropDownTextField();
}

class _DropDownTextField extends State<DropDownTextField> {
  final GlobalKey _globalKey = GlobalKey();

  double globPositionX = 0;
  double globPositionY = 0;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onTap: () {
            final BuildContext? context = _globalKey.currentContext;
            if (context != null) {
              final RenderBox? renderBox =
                  context.findRenderObject() as RenderBox?;
              if (renderBox != null) {
                final Offset globalPosition =
                    renderBox.localToGlobal(Offset.zero);
                globPositionX = globalPosition.dx;
                globPositionY = globalPosition.dy;
                print(
                    'Component position: X: $globPositionX, Y: $globPositionY');
              } else {
                print('RenderBox is null.');
              }
            } else {
              print('BuildContext is null.');
            }
            Navigator.of(context!).push(PopupWindow(
                child: Container(
                  width: widget.width ?? 130,
                  height: widget.list!.length * (widget.dialogItemHeight ?? 35),
                  constraints: BoxConstraints(maxHeight: 300),
                  color: widget.dialogColor ?? Colors.white,
                  child: ListView.separated(
                      itemBuilder: (context, index) {
                        return InkWell(
                            onTap: () {
                              setState(() {
                                Navigator.of(context).pop();
                                widget.controller!.text = widget.list![index];
                              });
                            },
                            child: Container(
                              margin: EdgeInsets.only(left: 10),
                              child: Text(
                                widget.list![index],
                                style: widget.dialogTextStyle ??
                                    TextStyle(color: Colors.black),
                              ),
                              height: widget.dialogItemHeight ?? 35,
                              alignment: Alignment.centerLeft,
                            ));
                      },
                      separatorBuilder: (context, index) {
                        return Divider(
                          height: 0.5,
                          color: Colors.grey,
                        );
                      },
                      itemCount: widget.list!.length),
                ),
                left: globPositionX,
                top: globPositionY + widget.height!));
          },
          child: Container(
              width: widget.width ?? 130,
              alignment: Alignment.center,
              key: _globalKey,
              margin: widget.margin,
              padding: widget.padding ?? EdgeInsets.symmetric(horizontal: 5),
              height: widget.height ?? 35,
              decoration: BoxDecoration(
                border: Border.all(
                  color: widget.boardColor ?? Colors.grey, // 边框颜色
                  width: 0.5, // 边框宽度
                ),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                      child: TextField(
                          controller: widget.controller,
                          keyboardType:
                              widget.textInputType ?? TextInputType.text,
                          onChanged: (value) {},
                          style: widget.textStyle,
                          decoration: InputDecoration(
                            hintText: '',
                            hintStyle: widget.hintStyle ??
                                TextStyle(fontSize: 14, color: Colors.grey),
                            contentPadding: EdgeInsets.only(bottom: 15),
                            border: InputBorder.none,
                          ))),
                  widget.icon ??
                      Icon(
                        Icons.arrow_drop_down_outlined,
                        color: Colors.black54,
                      )
                ],
              )),
        ),
      ],
    );
  }
}

class PopupWindow extends PopupRoute {
  final Widget child;
  final double? left;
  final double? top;

  PopupWindow({
    required this.child,
    this.left,
    this.top,
  });

  @override
  Duration get transitionDuration => const Duration(milliseconds: 100);

  @override
  String? get barrierLabel => null;

  @override
  Color? get barrierColor => null;

  @override
  bool get barrierDismissible => true;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    if (left == null || top == null) {
      return UnconstrainedBox(child: child);
    }
    return Stack(
      children: [
        Positioned(
          left: left,
          top: top,
          child: child,
        )
      ],
    );
  }
}
